using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Design;

namespace TinyERP.Framework.Controls
{
	public delegate void AfterRowSelectEventHandler(object sender, object SelectedItem);
	/// <summary>
	/// Summary description for MultiColumnComboPopup.
	/// </summary>
	internal class MultiColumnComboPopup : System.Windows.Forms.Form
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
        private System.ComponentModel.Container components = null;
		

		public event AfterRowSelectEventHandler AfterRowSelectEvent;
        private ComponentFactory.Krypton.Toolkit.KryptonDataGridView dataGridView;
       
       
        public DataGridView DataGridView
        {
            get
            {
                return this.dataGridView;
            }
        }
        public object DataSource
        {
            get
            {
                return this.dataGridView.DataSource;
            }
            set
            {
                this.dataGridView.DataSource = value;
            }
        }
        public object SelectedItem
        {
            get
            {
                if (this.dataGridView.SelectedRows == null || this.dataGridView.SelectedRows.Count == 0)
                {
                    return null;
                }
                return this.dataGridView.SelectedRows[0].DataBoundItem;
            }
            set
            {
                object selectedItem = value;
                if (selectedItem != null)
                {
                    foreach (DataGridViewRow row in this.dataGridView.Rows)
                    {
                        if (row.DataBoundItem == selectedItem)
                        {
                            row.Selected = true;
                        }
                    }
                }
            }
        }
		public MultiColumnComboPopup()
		{
			InitializeComponent();
            this.dataGridView.AutoGenerateColumns = false;
            this.dataGridView.AllowUserToAddRows = false;
            this.dataGridView.AllowUserToDeleteRows = false;
            
		}

		

		

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            
            dataGridView = new ComponentFactory.Krypton.Toolkit.KryptonDataGridView();
            ((System.ComponentModel.ISupportInitialize)(dataGridView)).BeginInit();
            this.SuspendLayout();
            // 
            // dataGridView
            // 
            dataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
            dataGridView.GridStyles.Style = ComponentFactory.Krypton.Toolkit.DataGridViewStyle.List;
            dataGridView.GridStyles.StyleBackground = ComponentFactory.Krypton.Toolkit.PaletteBackStyle.GridBackgroundList;
            dataGridView.GridStyles.StyleColumn = ComponentFactory.Krypton.Toolkit.GridStyle.List;
            dataGridView.GridStyles.StyleDataCells = ComponentFactory.Krypton.Toolkit.GridStyle.List;
            dataGridView.GridStyles.StyleRow = ComponentFactory.Krypton.Toolkit.GridStyle.List;
            dataGridView.Location = new System.Drawing.Point(0, 0);
            dataGridView.MultiSelect = false;
            dataGridView.Name = "dataGridView";
            dataGridView.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            dataGridView.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
            dataGridView.Size = new System.Drawing.Size(292, 153);
            dataGridView.StateCommon.BackStyle = ComponentFactory.Krypton.Toolkit.PaletteBackStyle.GridBackgroundList;
            dataGridView.TabIndex = 0;
            dataGridView.DoubleClick += new System.EventHandler(this.DataGridViewDoubleClick);
            dataGridView.KeyDown += new System.Windows.Forms.KeyEventHandler(this.OnDataGridKeyDown);
            // 
            // MultiColumnComboPopup
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(292, 153);
            this.ControlBox = false;
            this.Controls.Add(dataGridView);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.KeyPreview = true;
            this.Name = "MultiColumnComboPopup";
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
            this.Deactivate += new System.EventHandler(this.MultiColumnComboPopup_Deactivate);
            this.Leave += new System.EventHandler(this.MultiColumnComboPopup_Leave);
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.MultiColumnComboPopup_KeyDown);
            ((System.ComponentModel.ISupportInitialize)(dataGridView)).EndInit();
            this.ResumeLayout(false);

		}
		#endregion


		private void MultiColumnComboPopup_Deactivate(object sender, System.EventArgs e) {
            this.Hide();
		}

		private void MultiColumnComboPopup_Leave(object sender, System.EventArgs e) {
            this.Hide();
		}

		private void MultiColumnComboPopup_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
			if(e.KeyData==Keys.Escape){
				this.Hide();
			}
		}

		
		
        public int Cols {
			get {
                return this.dataGridView.ColumnCount;
			}
		}

		public int Rows {
			get {
                return this.dataGridView.RowCount;
			}
		}

        private void OnDataGridKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                if (dataGridView.SelectedRows.Count > 0)
                {
                    if (AfterRowSelectEvent != null)
                    {
                        AfterRowSelectEvent(this, this.SelectedItem);
                    }
                }
                this.Hide();
            }
        }

        private void DataGridViewDoubleClick(object sender, EventArgs e)
        {
            if (dataGridView.SelectedRows.Count > 0)
            {
                if (AfterRowSelectEvent != null)
                {
                    AfterRowSelectEvent(this, this.SelectedItem);
                }
            }
            this.Hide();
        }

	}
}
